clear all
import excel "D:\Paper New Ecuador\quarterly.xlsx", sheet("Hoja1") firstrow

*Data wrangle

drop DATE
generate date = tq(2000q1) + _n-1
format %tq date
tsset date
order date, first
label variable date "Date"
label variable Y "Real Output (Thousands of us dollars year 2007)"
label variable C "Real Household Consumption"
label variable G "Real Goverment Consumption"
label variable K "Capital"
label variable X "Exports"
label variable M "Imports"
label variable TB "Aggregate Trade Balance"
label variable OIL "Oil exports in thousands of barrils"
label variable Nrem "Nominal Remittances "
label variable CPI "Consumer Price Index"
label variable Rrem "Real remittances"
label variable Pop "Population"
label variable pcy "Per capita output"
label variable pcc "Per capita consumption"
label variable pcg "Per capita gov. consumption"
label variable pci "Per capita investment"
label variable pcx "Per capita exports"
label variable pcm "Per capita imports"
label variable pctb "Per capita trade balance"
label variable pcoil "Per capita oil exports"
label variable pcrem "Per capita remittances"
label variable lpcy "ln pcy"
label variable lpcc "ln pcc"
label variable lpcg "ln pcg"
label variable lpci "ln pci"
label variable lpcx "ln pcx"
label variable lpcm "ln pcm"
label variable lpcoil "ln pcoil"
label variable lpcrem "ln pcrem"
label variable exr "exchange rate"
label variable lexr "ln exr"
order exr, after(Rrem)
gen t = _n
gen t2 = t^2
label variable t "Determinist trend"
label variable t2 "Quadratic trend"

*Figure 3

gen LY = ln(Y)
label variable LY "ln Y"
tsline LY, ytitle("LN GDP")

*Table 1

*Finding peaks and throughs

gen max = 0
gen l1LY = L.LY
gen l2LY = L2.LY
gen f1LY = F.LY
gen f2LY = F2.LY
replace max = 1 if LY > l1LY & LY > l2LY & LY > f1LY & LY > f2LY
gen min = 0
replace min = 1 if LY < l1LY & LY < l2LY & LY < f1LY & LY < f2LY
browse date LY max min

*Amplitude expansion first cycle 
display ((13919627 - 9134587) / 9134587) * 100

*Amplitude contraction second cycle
display ((13579505 - 13919627) / 13919627) * 100

*Amplitude expansion second cycle
display ((17816050 - 13579505) / 13579505) * 100

*Amplitude contraction third cycle
display ((17204627 - 17816050 ) / 17816050) * 100

*Amplitude expansion third cycle
display ((18075353 - 17204627) / 17204627) * 100 

*Amplitude contraction third cycle 
display ((15504941 - 18075353) / 18075353) * 100

*Amplitude expansion last cycle
display ((18251544 - 15504941) / 15504941) * 100

*TABLE 2

*STANDARD DEVIATIONS

*Log Quadratic Detrending

*Output gap

quietly reg lpcy t t2
predict cycle_y, residuals
label variable cycle_y "Cycle of GDP pc"
quietly sum cycle_y
display r(sd)

*Household Consumption to Output ratio 

quietly reg lpcc t t2
predict cycle_c, residuals
label variable cycle_c "Consumption Cycle"
quietly sum cycle_c
display r(sd)
display .03149913 / .03555013

*Government Consumption to Output ratio

quietly reg lpcg t t2
predict cycle_g, residuals
label variable cycle_g "Government Cycle"
quietly sum cycle_g
display r(sd)
display .08863328 / .03555013

*Investment to Output ratio

quietly reg lpci t t2
predict cycle_i, residuals
label variable cycle_i "Investment Cycle"
quietly sum cycle_i
display r(sd)
display .08623957 / .03555013

*Exports to Output ratio

quietly reg lpcx t t2
predict cycle_x, residuals
label variable cycle_x "Exports Cycle"
quietly sum cycle_x
display r(sd)
display .05206709 / .03555013

*Imports to Output ratio

quietly reg lpcm t t2
predict cycle_m, residuals
label variable cycle_m "Imports Cycle"
quietly sum cycle_m
display r(sd)
display .0808152 / .03555013

*Trade Balance to Output ratio

gen tby = pctb / pcy 
label variable tby "Tb to output ratio"
quietly reg tby t t2
predict cycle_tby, residuals
label variable cycle_tby "Cycle of Tb to output ratio"
quietly sum cycle_tby
display r(sd)

*HP - filtered

gen gy = pcg/pcy
label variable gy "Share of Gov. Spending"
quietly reg lpcy t t2
predict dep
label variable dep "Trend of pc LGDP"
rename dep ys
gen den = exp(ys)
gen tb = pctb/den
drop den
label variable tb "Trade balance"
tsfilter hp hpy hpc hpg hpi hpx hpm hptby hpgy hptb = lpcy lpcc lpcg lpci lpcx lpcm tby gy tb, smooth(1600)

*In this table we find both sd(y) and sd(tby)
sum hpy hpc hpg hpi hpx hpm hptby 

*Household consumption to output ratio
display .0257971 / .022883

*Government consumptio to output ratio
display .0266662 / .022883

*Investment to output ratio
display .058978 / .022883

*Exports to output ratio
display .0389921 / .022883

*Imports to output ratio
display .0707089 / .022883

*First - Differenced

gen dlpcy = lpcy - L.lpcy
gen dlpcc = lpcc - L.lpcc
gen dlpcg = lpcg - L.lpcg
gen dlpci = lpci - L.lpci
gen dlpcx = lpcx - L.lpcx
gen dlpcm = lpcm - L.lpcm
label variable dlpcy "First difference of y"
label variable dlpcc "First difference of c"
label variable dlpcg "First difference of g"
label variable dlpci "First difference of i"
label variable dlpcx "First difference of x"
label variable dlpcm "First difference of m"

*In this table we find sd(y). Important: the value of the trade balance to output ratio is taken from the log-quadratic detrending as in Uribe & Smith-Grohé (2017)
sum dlpcy dlpcc dlpcg dlpci dlpcx dlpcm 

*Household consumption to output ratio
display .0202146 / .0186971

*Government consumptio to output ratio
display .0256874 / .0186971

*Investment to output ratio
display .0451554 / .0186971

*Exports to output ratio
display .0312711 / .0186971

*Imports to output ratio
display .0619058 / .0186971

*CORRELATIONS WITH Y

*Log Quadratic Detrending

quietly reg gy t t2
predict cycle_gy, residuals
label variable cycle_gy "Cycle of the Gov. Share"
quietly reg tb t t2
predict cycle_tb, residuals
label variable cycle_tb "Cycle of the TB"
pwcorr cycle_y cycle_c cycle_gy cycle_i cycle_x cycle_m cycle_tby cycle_tb 

*HP - Filtered

pwcorr hpy hpc hpgy hpi hpx hpm hptby hptb

*First - Differenced

pwcorr dlpcy dlpcc cycle_gy dlpci dlpcx dlpcm cycle_tby cycle_tb

*SERIAL CORRELATIONS

*Log Quadratic Detrending

correlate cycle_y L.cycle_y
correlate cycle_c L.cycle_c
correlate cycle_g L.cycle_g
correlate cycle_i L.cycle_i
correlate cycle_x L.cycle_x
correlate cycle_m L.cycle_m
correlate cycle_tby L.cycle_tby 

*HP - Filtered

correlate hpy L.hpy
correlate hpc L.hpc
correlate hpg L.hpg
correlate hpi L.hpi
correlate hpx L.hpx
correlate hpm L.hpm
correlate hptby L.hptby

*First - Differenced 

correlate dlpcy L.dlpcy
correlate dlpcc L.dlpcc
correlate dlpcg L.dlpcg 
correlate dlpci L.dlpci 
correlate dlpcx L.dlpcx 
correlate dlpcm L.dlpcm 
correlate cycle_tby L.cycle_tby 

order max l1LY l2LY f1LY f2LY min, last

*Cross correlations between each variable and output

xcorr hpy hpc, name(gra1)
xcorr hpy hpg, name(gra2)
xcorr hpy hpgy, name(gra3)
xcorr hpy hpi, name(gra4)
xcorr hpy hpx, name(gra5)
xcorr hpy hpm, name(gra6)
xcorr hpy hptby, name(gra7)
xcorr hpy hptb, name(gra8)
graph combine gra1 gra2 gra3 gra4 gra5 gra6 gra7 gra8, row(3) col(3)

*Determine by how much the tby and tb lead hpy
xcorr hpy hptby, table
xcorr hpy hptb, table 

*Table 3

*Dickey - Fuller unit root test

*Creation of dummy seasonal variables

generate q=quarter(dofq(date))
gen q1=(q==1)
gen q2=(q==2)
gen q3=(q==3)
label variable q "Seasonal factor"
label variable q1 "First quarter dummy"
label variable q2 "Second quarter dummy"
label variable q3 "Third quarter dummy"

*(the number of lags for the dependant follows the AIC criteria, for convenience the model selection was performed in Gretl and the selected specification is replied manually here)

reg D.lpcy L.lpcy t t2 
reg D2.lpcy D.L.lpcy t
reg D.lexr L.lexr D.L.lexr 
reg D2.lexr D.L.lexr 
reg D.lpcrem L.lpcrem q1 q2 q3 
reg D2.lpcrem D.L.lpcrem q1 q2 q3 
reg D.lpcoil L.lpcoil D.L.lpcoil t t2 
reg D2.lpcoil D.L.lpcoil l(1/4).D2.lpcoil t 

*Lag selection for the VARX model

varsoc D.lpcy D.lexr D.lpcrem, maxlag(8) exog(D.lpcoil q1 q2 q3) lutstats

*Estimation VARX model

*Using first difference

*Impulse - response and transfer functions

gen dlpcoil = lpcoil - L.lpcoil
quietly var D.lpcy D.lexr D.lpcrem, exog(dlpcoil q1 q2 q3)
irf set "myirf1", replace
irf create order1, step(12) order(D.lpcy D.lexr D.lpcrem) 
irf create order2, step(12) order(D.lpcrem D.lexr D.lpcy)  
irf ograph (order1 D.lexr D.lpcy oirf) (order2 D.lexr D.lpcy oirf), name(g1) 
irf ograph (order1 D.lpcrem D.lpcy oirf) (order2 D.lpcrem D.lpcy oirf), name(g2)
irf ograph (order1 dlpcoil D.lpcy dm) (order2 dlpcoil D.lpcy dm)
graph combine g1 g2, row(2) col(1)
irf ctable (order1 D.lexr D.lpcy fevd) (order2 D.lexr D.lpcy fevd) (order1 D.lpcrem D.lpcy fevd) (order2 D.lpcrem D.lpcy fevd) (order1 dlpcoil D.lpcy cdm) (order2 dlpcoil D.lpcy cdm), noci 











